package com.bgs.travelapp.config;

import com.bgs.travelapp.scheduledTasks.entity.ScheduledTasksEntity;
import com.bgs.travelapp.card.entity.CardRecordEntity;
import com.bgs.travelapp.card.service.CardRecordService;
import com.bgs.travelapp.order.entity.ScenicOrderEntity;
import com.bgs.travelapp.order.mapper.ScenicOrdersMapper;
import com.bgs.travelapp.scenic.entity.ScenicEntity;
import com.bgs.travelapp.scenic.mapper.ScenicMapper;
import com.bgs.travelapp.scenicRank.entity.ScenicRankEntity;
import com.bgs.travelapp.scenicRank.mapper.ScenicRankMapper;
import com.bgs.travelapp.scheduledTasks.mapper.ScheduledTaskMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 定时任务
 * todo 定时查询用户购买会员卡过期时间
 * todo 定时查询前端景区首页景区排行数据
 * gpd 2022-08-08 09点51分
 */
@Configuration
@EnableScheduling
public class ScheduledTasks {

    @Resource
    CardRecordService cardRecordService;

    @Resource
    ScenicMapper scenicMapper;

    @Resource
    ScenicRankMapper scenicRankMapper;

    @Resource
    ScenicOrdersMapper scenicOrdersMapper;

    @Resource
    ScheduledTaskMapper scheduledTaskMapper;

    /**
     * 每天9点修改景区排行信息
     * 查询用户购买会员卡是否过期
     * 前端---景区首页（景区排行）
     * 前端---我的会员卡
     * gpd 2022-08-23 19点45分
     */
    @Scheduled(cron = "0 0 9 * * *")
    public void scenicRankTasks() {
        Date date = new Date();
        //创建Calendar 的实例
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        ScheduledTasksEntity scheduledTasksEntity = new ScheduledTasksEntity();
        //查询定时任务的创建时间
        List<ScheduledTasksEntity> scheduledTasksEntities = scheduledTaskMapper.select(scheduledTasksEntity);
        //数据库中的定时任务的创建时间
        String dateTask ="";
        //今天的时间
        String today ="";
        for (ScheduledTasksEntity scheduledTasks : scheduledTasksEntities){
            dateTask = sdf.format(scheduledTasks.getCreateTime());
        }
        today = sdf.format(date);
        if(!dateTask.equals(today)){
            //查询会员卡购买记录
            List<CardRecordEntity> list = cardRecordService.getQueryOverdueTasks();
            ScenicEntity scenicEntity = new ScenicEntity();
            Example example = new Example(ScenicOrderEntity.class);
            example.createCriteria().andEqualTo("orderStatus",3).orEqualTo("orderStatus",4);
            //查询景区表收藏数
            List<ScenicEntity> scenicEntities = scenicMapper.select(scenicEntity);
            //查询出行表用户出行景区次数
            List<ScenicOrderEntity> scenicOrderEntities = scenicOrdersMapper.selectByExample(example);
            //查询用户会员卡是否过期
            //如果过期就将会员卡过期字段修改
            for (CardRecordEntity cardRecordEntity : list) {
                if (cardRecordEntity.getExpirationTime().before(calendar.getTime())) {
                    cardRecordService.UpdateOverdueTasks(cardRecordEntity.getId());
                }
            }
            //更新景区排行收藏景区的数据
            for (ScenicEntity scenic : scenicEntities) {
                int i = scenicRankMapper.updateScenicCollection(scenic.getCollection(),scenic.getId());
            }
            //更新景区排行出行景区的数据
            for(ScenicOrderEntity scenicOrderEntity :scenicOrderEntities){
                Integer ticketSum=scenicOrdersMapper.scenicStatistics(scenicOrderEntity.getScenicId());
                int i = scenicRankMapper.updateScenicTraveQuantity(scenicOrderEntity.getScenicId(),ticketSum);
            }
            String dateStr = sdf.format(date);
            //更改定时任务的时间
            try {
                for (ScheduledTasksEntity scheduledTasks : scheduledTasksEntities){
                    scheduledTasks.setId(scheduledTasks.getId());
                    scheduledTasks.setCreateTime(sdf.parse(dateStr));
                    int i = scheduledTaskMapper.updateByPrimaryKeySelective(scheduledTasks);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }else{
            System.out.println("今日已执行定时任务：修改景区排行榜数据，用户会员卡过期时间");
        }

    }
}
